function PlotHitRates(ax,schedule)
%%

wp = schedule.writeparams;
rp = schedule.readparams;
rv = schedule.response_vals(1:end-1,:);
rc = rv(:,2);
% uttidx = findincell(ismember(wp,'TrialType'));
% utt = cell2mat(schedule.data(:,uttidx));

clear data

data.Quiet.bit = 11; data.Quiet.plotcode = '.b';
data.Tone.bit  = 12; data.Tone.plotcode  = 'sg';
data.AM.bit    = 13; data.AM.plotcode    = '*r';

fn = fieldnames(data);
for i = 1:length(fn)
    b = data.(fn{i}).bit;
    data.(fn{i}).ind = bitget(rc,b);
    n = sum(data.(fn{i}).ind);
    data.(fn{i}).avoids = bitget(rc,1)&data.(fn{i}).ind;
    data.(fn{i}).avoidrate = sum(data.(fn{i}).avoids)/n*100;
    y = cumsum(data.(fn{i}).avoids)./cumsum(data.(fn{i}).ind);
    data.(fn{i}).runningavoidrate = y(find(data.(fn{i}).ind));
end

f = findobj('type','figure','-and','Tag','SchResultsFig');
if isempty(f), f = figure('Tag','SchResultsFig'); end
clf(f);


cla(ax)
hold on
k = 1; clear lstr
for i = 1:length(fn)
    x = find(data.(fn{i}).ind);
    plot(ax,x,data.(fn{i}).runningavoidrate,data.(fn{i}).plotcode);
    if ~isempty(x), lstr{k} = sprintf('%s (%0.1f%%)',fn{i},data.(fn{i}).avoidrate); k = k + 1; end %#ok<SAGROW>
end
hold off
ylim([0 1]);
grid on
box on
legend(lstr)
title('Running Hit Rate');
xlabel('Trial number');
ylabel('Hit rate');
